﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SlimDX.DirectInput;

namespace WheelChairSimulation2
{
    class WheelChair
    {
        public Texture2D wheelChairTexture;
        private Vector2 position;
        private Vector2 direction;
        public Vector2 origin;
        public float wheelChairAngle = 0;
        public float wheelChairScale = 0.5f;

        //private MouseState mouseState;
        private Microsoft.Xna.Framework.Input.KeyboardState keyBoard;
        List<Joystick> joysticks = new List<Joystick>();
        List<DeviceInstance> directInputList = new List<DeviceInstance>();
        DirectInput directInput = new DirectInput();
        JoystickState joystickState = new JoystickState();

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        public WheelChair(SimEnvironment environment, Vector2 position)
        {
            this.position = position;
            

            wheelChairTexture = environment.content.Load<Texture2D>("malewheelChair");

            // origin that is used for rotation axis.
            origin = new Vector2((wheelChairTexture.Width / 2), (wheelChairTexture.Height / 2));

            directInputList.AddRange(directInput.GetDevices(SlimDX.DirectInput.DeviceClass.GameController, SlimDX.DirectInput.DeviceEnumerationFlags.AttachedOnly));

            foreach (SlimDX.DirectInput.DeviceInstance obj in directInputList)
            {
                //joysticks.Capacity++;
                joysticks.Add(new SlimDX.DirectInput.Joystick(directInput, obj.InstanceGuid));
                joysticks[joysticks.Count - 1].Acquire();
            }
        }
        public WheelChair(Texture2D texture)
        {

        }
        public void Initialize(ContentManager content)
        {

        }
        public void Update(GameTime gameTime)
        {
            //mouseState = Mouse.GetState();
            keyBoard = Microsoft.Xna.Framework.Input.Keyboard.GetState();
            
            //if (keyBoard.IsKeyDown(Keys.F1))


            if (joysticks.Count > 0)
            {
                //joystickState = joysticks
                joystickState = joysticks[0].GetCurrentState();
                //joystickState = joysticks[0].GetForceSliders();

                //if (joystickState.X > 34000)
                //    wheelChairAngle += .02f;// rotate right.
                //else if (joystickState.X < 31000)
                //    wheelChairAngle -= .02f;// rotate left.

                //if (joystickState.Y > 34000)
                //    position.Y += 2;
                //else if (joystickState.Y < 31000)
                //    position.Y -= 2;
                //joystickState.

                
                // midde region = 32767
                if (joystickState.X > 37877)
                {
                    //direction.X += 2;
                    wheelChairAngle += .01f; // rotate right.
                }
                else if (joystickState.X < 28657)
                {
                    //direction.X += 2;
                    wheelChairAngle -= .01f; // rotate left.
                }

                Matrix rotMatrix = Matrix.CreateRotationZ(wheelChairAngle);
                
                if (joystickState.Y > 34967)
                {
                    // move backward
                    //direction.Y += 2;
                    Vector2 backward = WheelChairMove(joystickState.Y);
                    direction = Vector2.Transform(backward, rotMatrix);
                }
                else if (joystickState.Y < 30167)
                {
                    // move forward
                    //direction.Y -= 2;

                    Vector2 forward = WheelChairMove(joystickState.Y);
                    direction = Vector2.Transform(forward, rotMatrix);
                }

                position += direction;
                direction.X = 0;
                direction.Y = 0;
            }

        }

        protected Vector2 WheelChairMove(int joystickStateY)
        {
            Vector2 move;
            if (joystickStateY > 32767)
            {
                float speed = joystickStateY / 25000;
                move = new Vector2(0, speed);
            }
            else if (joystickStateY < 30167)
            {
                float speed = (-3f/32767f)*joystickStateY + 3;
                move = new Vector2(0, -speed);
            }
            else
                move = new Vector2(0, 0);
            return move;

        }

        protected void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            directInput.Dispose();
            foreach (Joystick joystick in joysticks)
            {
                joystick.Dispose();
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            //spriteBatch.Begin();
            //spriteBatch.Draw(wheelChairTexture, position, Color.White);
            //position.X = position.X + wheelChairTexture.Width / 2;
            //position.Y = position.Y + wheelChairTexture.Height / 2;
            //new Vector2(0, 0)
            Vector2 position2 = new Vector2(position.X + origin.X, position.Y + origin.Y);
            
            spriteBatch.Draw(wheelChairTexture, position, null, Color.White, wheelChairAngle, origin, wheelChairScale, SpriteEffects.None, 0.0f);
            //spriteBatch.End();
        }
    }
}

